/*************************************************************************
    > File Name: 4.pthread_join.c
    > Author: Ykk
    > Mail: 3393579535@qq.com 
    > Created Time: 2023年05月10日 星期三 19时39分59秒
 ************************************************************************/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include<unistd.h>
#include<pthread.h>

//线程处理函数
void *fun(void *arg)
{
	int i = 0;
	for(i = 0;i < 5;i++)
	{
		printf("fun thread do working %d\n",i);
		sleep(1);
	}
	return (void *)0x3;
}
//回收线程资源
int main()
{
	int ret = -1;
	void *retp = NULL;
	pthread_t tid = -1;

	//创建一个线程
	ret = pthread_create(&tid,NULL,fun,NULL);
	if(0 != ret)
	{
		printf("pthread_create failed....\n");
		return 1;
	}

	printf("main thread running ...\n");

	//等待线程结束 会阻塞	
	ret = pthread_join(tid,&retp);
	if(0 != ret)
	{
		printf("pthread_join failed....\n");
		return 1;
	}
	printf("retp: %p\n",retp);
	printf("main thread exit ...\n");

	return 0;

}
